function menuTree() {
//获取菜单数据
    var dataJson = [{
        "name": "My Module",
        "code": "GZT",
        "icon": "",
        "child": [
            {
                "name": "My Module",
                "icon": "fa-cloud-upload",
                "code": "FXDJ",
                "url": "index/Risk/risk_list",
                "parentCode": "GZT"
            }
            
        ]
    },
        {
            "name": "My Score",
            "code": "FXJC",
            "icon": "",
            "child": [
                {
                    "name": "My Score",
                    "code": "JCGK",
                    "url": "monitor_index.html",
                    "icon": "fa-filter",
                    "parentCode": "FXJC"
                }
                
            ]
        }
        ,
        {
            "name": "Module Info",
            "code": "FXYJ",
            "icon": "",
            "child": [
                {
                    "name": "All Modules",
                    "code": "YJGK",
                    "url": "index/Port/port_list",
                    "icon": "fa-bar-chart",
                    "parentCode": "FXYJ"
                },
                {
                    "name": "Available Modules",
                    "code": "YJGK",
                    "url": "index/Policy/policy_list",
                    "icon": "fa-bar-chart",
                    "parentCode": "FXYJ"
                },
                {
                    "name": "Selected Modules",
                    "code": "YJGK",
                    "url": "",
                    "icon": "fa-bar-chart",
                    "parentCode": "FXYJ"
                },
                {
                    "name": "Conflicting Modules",
                    "code": "YJGK",
                    "url": "",
                    "icon": "fa-bar-chart",
                    "parentCode": "FXYJ"
                }
            ]
        }
       
        ,
        {
            "name": "About Me",
            "code": "QYXX",
            "icon": "",
            "child": [{
                "name": "基础信息",
                "code": "JCXX",
                "url": "company_list.html",
                "icon": "fa-edit",
                "parentCode": "QYXX"
            }]
        }
        
        
        
        
        
        
    ];
    showAll(dataJson);

    function encodeURL(url) {
        if (url) {
            var reg = new RegExp("(.*)\\?(.*)\\?(.*)");
            url = url.replace(reg, "$1?$2&$3");
            return encodeURI(url);
        }
    }

    function showAll(data) {
        var $mainList = $("#header-menu");
        var $itemList = $("#side-menu");
        $.each(data, function (index, obj) {
            var $headMennuList = buildHeadItem(index, obj);
            $mainList.append($headMennuList);
            var $tabDiv = $("<div class='tab-menu animated fadeIn tabMenu" + index + "'/>");
            if (index == 0) {
                $tabDiv.addClass('active');
            }
            if (obj.child) {
                $.each(obj.child, function (index, obj) {
                    var $leftMennuList = buildItem(obj);
                    $tabDiv.append($leftMennuList);
                    $itemList.append($tabDiv);
                });
            }
        })
    }

    function buildHeadItem(index, itemDate) {
        var $li = $("<li/>");
        var $link = $("<a onclick=\"changeMenuItem(" + index + ")\" title=" + itemDate.name + ">" + itemDate.name + "<i></i></a>");
        if (index == 0) {
            $link.attr('class', 'on');
        }
        if (itemDate.child) {
            // 有子菜单的节点
            buildItem(itemDate.child);
        }
        $li.append($link);
        return $li;
    }

    function buildItem(itemDate) {
        var $li = $("<li/>");
        var $link = $("<a class=\"J_menuItem\" title=" + itemDate.name + " href=" + itemDate.url + "><i class='fa " + itemDate.icon + "'></i><span class=\"nav-label\">" + itemDate.name + "</span></a>");
        var $arrow = $("<span class=\"fa arrow\"></span>");
        $li.append($link);
        if (itemDate.child) {
            // 有子菜单的节点
            $arrow.prependTo($link);
            var secondMenu = buildSubMenu(itemDate.child);
            $li.append(secondMenu);
        }
        return $li;
    }

    function buildSubMenu(childrens, parentTxt) {
        var $ul = $("<ul class='nav nav-second-level' />");
        $.each(childrens, function (index, obj) {
            if (obj.child) {
                // 继续构建下级节点
                // 下级节点的容器
                var $li = $("<li/>");
                var $link = $("<a><span class='nav-label'>" + this.name + "</span><span class='fa arrow'></a>");
                $li.append($link);
                var $subNav = buildSubMenu(this.child, parentTxt + " > " + this.name);
                $li.append($subNav);
                $ul.append($li);
                //
            } else {
                var $li = $("<li/>");
                var action = encodeURL(this.url);
                var $link = $("<a class=\"J_menuItem\" title=" + this.name + " href=" + action + ">" + this.name + "</a>");
                $li.append($link)
                $ul.append($li);
            }
        });
        return $ul
    }
}

menuTree();


